/// 时间显示工具类 - 用于格式化时间显示
/// 遵循马甲包开发规范，使用非常规命名避免重复识别
class TimeDisplayHelper {
  /// 格式化时间显示
  /// 规则：
  /// - 小于一天：显示具体时间 如 11:11
  /// - 等于一天：显示"昨天"
  /// - 大于一天小于等于七天：显示周几
  /// - 大于七天：显示具体日期
  static String formatTimeDisplay(DateTime targetTime) {
    final now = DateTime.now();
    final today = DateTime(now.year, now.month, now.day);
    final targetDate = DateTime(targetTime.year, targetTime.month, targetTime.day);
    
    final daysDifference = today.difference(targetDate).inDays;
    
    if (daysDifference == 0) {
      // 今天 - 显示具体时间
      return _formatTimeOnly(targetTime);
    } else if (daysDifference == 1) {
      // 昨天
      return 'Yesterday';
    } else if (daysDifference <= 7) {
      // 一周内 - 显示周几
      return _getWeekdayName(targetTime.weekday);
    } else {
      // 超过一周 - 显示具体日期
      return _formatDateOnly(targetTime);
    }
  }
  
  /// 格式化时间部分 (HH:mm)
  static String _formatTimeOnly(DateTime dateTime) {
    final hour = dateTime.hour.toString().padLeft(2, '0');
    final minute = dateTime.minute.toString().padLeft(2, '0');
    return '$hour:$minute';
  }
  
  /// 获取周几的英文名称
  static String _getWeekdayName(int weekday) {
    switch (weekday) {
      case 1:
        return 'Monday';
      case 2:
        return 'Tuesday';
      case 3:
        return 'Wednesday';
      case 4:
        return 'Thursday';
      case 5:
        return 'Friday';
      case 6:
        return 'Saturday';
      case 7:
        return 'Sunday';
      default:
        return 'Unknown';
    }
  }
  
  /// 格式化日期部分 (MM/dd)
  static String _formatDateOnly(DateTime dateTime) {
    final month = dateTime.month.toString().padLeft(2, '0');
    final day = dateTime.day.toString().padLeft(2, '0');
    return '$month/$day';
  }
  
  /// 获取详细的时间信息（用于调试或其他用途）
  static String getDetailedTimeInfo(DateTime targetTime) {
    final now = DateTime.now();
    final difference = now.difference(targetTime);
    
    if (difference.inMinutes < 1) {
      return 'Just now';
    } else if (difference.inHours < 1) {
      return '${difference.inMinutes} minutes ago';
    } else if (difference.inDays < 1) {
      return '${difference.inHours} hours ago';
    } else {
      return '${difference.inDays} days ago';
    }
  }
  
  /// 判断是否为今天
  static bool isToday(DateTime targetTime) {
    final now = DateTime.now();
    final today = DateTime(now.year, now.month, now.day);
    final targetDate = DateTime(targetTime.year, targetTime.month, targetTime.day);
    
    return today.isAtSameMomentAs(targetDate);
  }
  
  /// 判断是否为昨天
  static bool isYesterday(DateTime targetTime) {
    final now = DateTime.now();
    final yesterday = DateTime(now.year, now.month, now.day - 1);
    final targetDate = DateTime(targetTime.year, targetTime.month, targetTime.day);
    
    return yesterday.isAtSameMomentAs(targetDate);
  }
  
  /// 判断是否在本周内
  static bool isThisWeek(DateTime targetTime) {
    final now = DateTime.now();
    final today = DateTime(now.year, now.month, now.day);
    final targetDate = DateTime(targetTime.year, targetTime.month, targetTime.day);
    
    final daysDifference = today.difference(targetDate).inDays;
    return daysDifference >= 0 && daysDifference <= 7;
  }
  
  /// 格式化完整的日期时间（备用方法）
  static String formatFullDateTime(DateTime dateTime) {
    final year = dateTime.year;
    final month = dateTime.month.toString().padLeft(2, '0');
    final day = dateTime.day.toString().padLeft(2, '0');
    final hour = dateTime.hour.toString().padLeft(2, '0');
    final minute = dateTime.minute.toString().padLeft(2, '0');
    
    return '$year/$month/$day $hour:$minute';
  }
}
